const Controller = require('egg').Controller;

class RecruitController extends Controller {
  /**
     *
     * @api {POST} /api/recruit/create 创建招聘信息
     * @apiName 创建招聘信息
     * @apiGroup 招聘模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {String} name 岗位名称
     * @apiParam  {String} requirement 岗位要求
     * @apiParam  {String} desc 岗位描述
     * @apiParam  {String} salary 岗位待遇
     * @apiParam  {String} phone 联系方式
     * @apiParam  {String} address 地址
     *
     * @apiParamExample  {type} Request-Example:
        {
            "name": "name",
            "requirement": "requirement",
            "desc": "desc",
            "salary": "salary",
            "phone": "13622222222",
            "address": "address"
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 4,    //招聘id
                "name": "name", //岗位名称
                "requirement": "requirement",   //岗位要求
                "desc": "desc", //岗位描述
                "salary": "salary", //岗位待遇
                "phone": "13622222222", //联系方式
                "address": "address",   //地址
                "projectId": 1, //所属项目id
                "updatedAt": "2021-11-11T11:26:12.545Z",
                "createdAt": "2021-11-11T11:26:12.545Z"//创建时间
            }
        }
     *
     *
     */
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'name',
      'requirement',
      'desc',
      'salary',
      'phone',
      'address'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.recruit.create();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     *
     * @api {DELETE} /api/news/delete 删除招聘信息
     * @apiName 删除招聘信息
     * @apiGroup 招聘模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 招聘id
     *
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  async delete() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.recruit.delete();
      if (res == 'id不存在,删除失败') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     *
     * @api {PUT} /api/recruit/update 修改招聘信息
     * @apiName 修改招聘信息
     * @apiGroup 招聘模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {Number} id 招聘id
     * @apiParam  {String} name 岗位名称
     * @apiParam  {String} requirement 岗位要求
     * @apiParam  {String} desc 岗位描述
     * @apiParam  {String} salary 岗位待遇
     * @apiParam  {String} phone 联系方式
     * @apiParam  {String} address 地址
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "id":5, //招聘id
            "name": "name2",    //岗位名称
            "requirement": "requirement2",  //岗位要求
            "desc": "desc2",    //岗位描述
            "salary": "salary2",    //岗位待遇
            "phone": "13622222222", //联系方式
            "address": "address2"   //地址
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "修改成功",
            "data": []
        }
     *
     *
     */

  async update() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'id',
      'name',
      'requirement',
      'desc',
      'salary',
      'phone',
      'address'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.recruit.update();
        if (res == 'id不存在,修改失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     *
     * @api {GET} /api/recruit/getAll?limit=10&offset=0 获取所有招聘信息
     * @apiName 获取所有招聘信息
     * @apiGroup 招聘模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} limit 招聘信息返回数
     * @apiParam  {Number} offset 招聘信息偏移数
     *
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 3, //招聘信息总数
                "rows": [
                    {
                        "id": 1,    //招聘id
                        "name": "name", //岗位名称
                        "requirement": "requirement",   //岗位要求
                        "desc": "desc", //岗位描述
                        "salary": "salary", //岗位待遇
                        "phone": "13622222222", //联系方式
                        "address": "address",   //地址
                        "projectId": 1, //所属项目id
                        "createdAt": "2021-11-11T07:53:06.000Z",//创建时间
                        "updatedAt": "2021-11-11T07:53:06.000Z"
                    },
                    {
                        "id": 3,
                        "name": "name2",
                        "requirement": "requirement2",
                        "desc": "desc2",
                        "salary": "salary2",
                        "phone": "13622222223",
                        "address": "address2",
                        "projectId": 1,
                        "createdAt": "2021-11-11T07:59:52.000Z",
                        "updatedAt": "2021-11-11T08:00:21.000Z"
                    },
                    {
                        "id": 5,
                        "name": "name2",
                        "requirement": "requirement2",
                        "desc": "desc2",
                        "salary": "salary2",
                        "phone": "13622222222",
                        "address": "address2",
                        "projectId": 1,
                        "createdAt": "2021-11-11T11:31:04.000Z",
                        "updatedAt": "2021-11-11T11:31:41.000Z"
                    }
                ]
            }
        }
     *
     *
     */
  async getAll() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.recruit.getAll();
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     *
     * @api {GET} /api/recruit/getById?id=3 根据id查找招聘信息
     * @apiName 根据id查找招聘信息
     * @apiGroup 招聘模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {Number} id 招聘id
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 3,    //招聘id
                "name": "name2",    //岗位名称
                "requirement": "requirement2",  //岗位要求
                "desc": "desc2",    //岗位描述
                "salary": "salary2",    //岗位待遇
                "phone": "13622222223", //联系方式
                "address": "address2",  //地址
                "projectId": 1, //所属项目id
                "createdAt": "2021-11-11T07:59:52.000Z",    //创建时间
                "updatedAt": "2021-11-11T08:00:21.000Z"
            }
        }
     *
     *
     */

  async getById() {
    const { ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.recruit.getById();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }
}

module.exports = RecruitController;
